package hidalgo.expenses.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Util {

	public static final SimpleDateFormat SQL_DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	//public static final SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

	public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(
			"dd-MM-yyyy");

	public static String getDateString(int day, int month, int year) {
		return new StringBuilder().append(day).append("-").append(month + 1)
				.append("-").append(year).append(" ").toString();
	}
	
	public static DecimalFormat moneyFormat = new DecimalFormat("0.00");

	public static BroadcastReceiver connectionReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			boolean noConnectivity = intent.getBooleanExtra(
					ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
			String reason = intent
					.getStringExtra(ConnectivityManager.EXTRA_REASON);
			boolean isFailover = intent.getBooleanExtra(
					ConnectivityManager.EXTRA_IS_FAILOVER, false);

			NetworkInfo currentNetworkInfo = (NetworkInfo) intent
					.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
			NetworkInfo otherNetworkInfo = (NetworkInfo) intent
					.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

			// do application-specific task(s) based on the current network
			// state, such
			// as enabling queuing of HTTP requests when currentNetworkInfo is
			// connected etc.

			Log.d("INTERNET", "CAMBIO DE ESTADO");

			if (noConnectivity) {
				Log.d("INTERNET", "SIN INTERNET debido a: " + reason);
			} else {
				Log.d("INTERNET", "CON INTERNET debido a: " + reason);
			}

		}
	};
	
	public static boolean isOnline(Context context) {

		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// test for connection
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {
			Log.v("Util.java", "Internet Connection Not Present");
			return false;
		}

	}

	/**
	 * Example: format "2010-10-15T09:27:37Z". SimpleDateFormat(
	 * "yyyy-MM-dd'T'HH:mm:ss'Z'");
	 * 
	 * @return
	 */
	public static Date getDateFromString(String dateString,
			SimpleDateFormat simpleDateFormat) {

		Date date = null;
		try {
			date = simpleDateFormat.parse(dateString);
			System.out.println(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	public static String getStringFromDate(Date date,
			SimpleDateFormat simpleDateFormat) {

		String str = "";
		str = simpleDateFormat.format(date);
		System.out.println(str);

		return str;
	}



}
